package com.ym.manager.yinsheng;

import com.alibaba.fastjson.JSON;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.ym.manager.admin.properties.Config;
import com.ym.manager.yinsheng.util.RequestData;
import com.ym.manager.yinsheng.util.ServiceEnum;
import com.ym.manager.yinsheng.util.WeChatBotUtils;
import com.ym.manager.yinsheng.util.YinShengClient;
import com.ym.utils.JsonUtil;
import com.ym.utils.Logger;
import com.ym.utils.StringHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: YinShengMoneyManager
 * @Date: 2022/8/15 11:00
 * @Description:平台银盛账户余额查询--微信发送消息
 * @Version: 1.0
 */
//@Component
public class YinShengMoneyWechatManager {
//    @Scheduled(cron="0 0/10 * * * ? ") //间隔20分钟执行
    private void load() {
        Logger.info("平台银盛账户余额查询--------------------开始");
        float money = 0.00f;
        try {
            Map<String,Object> requestMap = new HashMap<>();
            requestMap.put("merchantNo", StringHandler.getString(Config.pp.get("ys.merchant.id"),"826691148160089"));
            RequestData requestData = new RequestData(requestMap);
            YinShengClient yinShengClient = new YinShengClient(ServiceEnum.QUERY_AMOUNT);
            String callback = yinShengClient.requestStrApi(requestData);
            if(StringHandler.isEmpty(callback)){
                Logger.info("YinShengMoneyManager","账户余额查询为空",new Exception());
                return;
            }
            Map<String, Object> obj = JsonUtil.jsonToBean(callback, Map.class);
            String cashAmount = "";
            if (!obj.get("code").equals("SYS000")) {
                Logger.error("账户余额查询失败:" + StringHandler.getString(obj.get("msg")),new Exception());
                return;
            } else {
                if (StringHandler.isNotEmpty(StringHandler.getString(obj.get("bizResponseJson")))) {
                    Map<String, Object> bizResponseJson = JsonUtil.jsonToBean(JsonUtil.toJson(obj.get("bizResponseJson")), Map.class);
                    if (bizResponseJson.get("subCode").equals("COM000")) {
                        List<Map<String, Object>> accountList = JsonUtil.jsonToBean(JsonUtil.toJson(bizResponseJson.get("accountList")), List.class);
                        if(StringHandler.isNotEmpty(accountList)){
                            for (Map<String, Object> accountMap: accountList){
                                if("01".equals(StringHandler.getString(accountMap.get("accountType")))){
                                    cashAmount = StringHandler.getString(accountMap.get("cashAmount"));
                                }
                            }
                        }
                    }  else {
                        Logger.error("账户余额查询失败:" + StringHandler.getString(bizResponseJson.get("subMsg")),new Exception());
                        return;
                    }
                } else {
                    Logger.error("账户余额查询失败:返回信息为空",new Exception());
                    return;
                }
            }
            money = StringHandler.getFloat(cashAmount,0.00f);
            String text = "银盛后台账户余额报警:当前余额￥"+money;
            String botUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fd94cd82-f630-4b32-bb1a-218ba0e8319f";
            WeChatBotUtils weChatBot = new WeChatBotUtils(botUrl, false);
// 发送文本消息
            String reqBody = weChatBot.sendTextMsg(text);
//            if(!response.isSuccess()){
//                Logger.error("银盛后台账户余额报警失败"+ JsonUtil.toJson(response),new Exception());
//            }
            if(StringHandler.isNotEmpty(reqBody)){
                Map<String,Object> map = JSON.parseObject(reqBody);
                if(!map.get("errmsg").equals("ok")){
                    Logger.error("银盛后台账户余额报警失败:"+ reqBody,new Exception());
                }
            }else{
                Logger.error("银盛后台账户余额报警失败:"+ reqBody,new Exception());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.info("平台银盛账户余额查询-----------("+money+")---------结束");
    }
}
